using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Ent.Web.Helper;
using Ent.Voice.Nomination;
using System.Collections.Generic;
using Ent.Web;
using Ent.Web.Skinner;
using Ent.Voice.Show;

public partial class Nominate : PageSkinner, IPageWizard
{
    public string GetTitle(Dictionary<string, object> values)
    {
        return (values != null && values.ContainsKey(NominationProperty.Title) ? values[NominationProperty.Title].ToString() : "New nomination");
    }
    public string SuccessCode { get { return  null; } }
    public string ErrorCode { get { return "ErrorNominationCreation"; } }
    public string Operation 
    { 
        get
        {
            return "Create Nomination";
        } 
    }
    public bool CanProcessWizard
    {
        get
        {
            try
            {
                int id = MathFunction.ConvertToInteger(Request.QueryString["id"]);
                UserNomination uNom = Nomination.GetUserNomination(id, Session["USERID"].ToString());
                Skin_wizard wiz = ((Skin_wizard)this.Master);
                switch (uNom.Status)
                {
                    case NominationStatus.Allowed:
                        Session["ALLOWEDFILES"] = uNom.FileTypes;
                        return true;
                    case NominationStatus.Nominated:
                        Response.Redirect("ShowEntry.aspx?id=" + uNom.EntryId.ToString(), false);
                        return false;
                    case NominationStatus.NotAllowed:
                        wiz.SetMessage(Common.GetErrorText(ResourceHelper.Get("MSG_Nom_NotAllowed")));
                        return false;
                    case NominationStatus.DueDateOver:
                        wiz.SetMessage(Common.GetErrorText(ResourceHelper.Get("MSG_Nom_DueDateOver")));
                        return false;
                    case NominationStatus.MaxCountOver:
                        wiz.SetMessage(Common.GetErrorText(ResourceHelper.Get("MSG_Nom_MaxCountOver")));
                        return false;
                    case NominationStatus.InsufficientCredit:
                        wiz.SetMessage(Common.GetErrorText(ResourceHelper.Get("MSG_Nom_InsufficientCredit")));
                        return false;
                    case NominationStatus.NotStarted:
                        wiz.SetMessage(Common.GetErrorText(ResourceHelper.Get("MSG_Nom_NotStarted")));
                        return false;
                }
            }
            catch (Exception exc)
            {
                Common.ErrorPage(exc);
            }
            return false;
        }
    }
    public void SetWizard(Ent.Web.WebControl.UserControl c)
    {
    }
    public bool ProcessWizardData(Dictionary<string, object> values)
    {
        if (Session["EVENTID"] == null)
            throw new Exception("Event not selected.");
        Nomination nData = new Nomination();
        nData.EventId = (int)Session["EVENTID"];
        nData.UserId = Session["USERID"].ToString();
        nData.Title = (values.ContainsKey(NominationProperty.Title) ? values[NominationProperty.Title].ToString() : "");
        nData.Description = (values.ContainsKey(NominationProperty.Description) ? values[NominationProperty.Description].ToString() : "");
        nData.FileName = (values.ContainsKey(NominationProperty.FileName) ? values[NominationProperty.FileName].ToString() : "");
        nData.Height = (values.ContainsKey(NominationProperty.Height) ? (int)values[NominationProperty.Height] : 0);
        nData.Width = (values.ContainsKey(NominationProperty.Width) ? (int)values[NominationProperty.Width] : 0);
        nData.Type = (values.ContainsKey(NominationProperty.Type) ? ((EventFileType)values[NominationProperty.Type]) : EventFileType.Text);
        nData.TextContent = (values.ContainsKey(NominationProperty.TextContent) ? values[NominationProperty.TextContent].ToString() : "");
        nData.Invitees = values.ContainsKey(EventProperty.Invitees) ? values[EventProperty.Invitees].ToString() : "";
        NominationResponse res = Nomination.Create(nData);
        if (res == NominationResponse.NominationCreated)
        {
            Ent.Web.Mail.Processor p = new Ent.Web.Mail.Processor();
            Ent.Web.Mail.MailContent cont = Ent.Web.Mail.Collection.GetMailContent("NominationCreated");
            p.To = Session["EMAIL"].ToString();
            p.Subject = cont.Subject;
            p.Body = string.Format(cont.Body, Session["FNAME"].ToString(), nData.Title, ConfigurationManager.AppSettings.Get("BaseUrl") + "/ShowEntry.aspx?id=" + nData.EntryId.ToString());
            p.SendMail();

            string[] emails = nData.Invitees.Split(new char[] { ',', ';', ' ', '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
            foreach (string email in emails)
            {
                if (Ent.Web.Mail.Processor.IsValidEmail(email))
                {
                    p = new Ent.Web.Mail.Processor();
                    cont = Ent.Web.Mail.Collection.GetMailContent("InviteVoters");
                    p.To = email;
                    p.Subject = string.Format(cont.Subject, nData.Title);
                    p.Body = string.Format(cont.Body, Session["FNAME"].ToString(), nData.Title, ConfigurationManager.AppSettings.Get("BaseUrl") + "/ShowEntry.aspx?id=" + nData.EntryId.ToString());
                    p.SendMail();
                }
            }
        }
        else
            return false;
        Session["ALLOWEDFILES"] = null;
        Session["CONTYPE"] = null;
        Response.Redirect(ConfigurationManager.AppSettings["BaseUrl"] + "/ShowEntry.aspx?id=" + nData.EntryId.ToString(), false);
        return true;
    }
}
